package com.xydtech.ecard.custgroup.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.custgroup.domain.EcardCustomerGroupInfo;
import com.xydtech.ecard.custgroup.service.IEcardCustomerGroupInfoService;
import com.xydtech.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行业客户配置Controller
 *
 * @author yuzhiguo
 * @date 2024-11-26 下午1:21
 */
@Controller
@RequestMapping("/ecard/customerGroup")
public class EcardCustomerGroupInfoController extends BaseController
{
    private String prefix = "ecard/custgroup/customerGroup";

    @Autowired
    private IEcardCustomerGroupInfoService ecardCustomerGroupInfoService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("ecard:customerGroup:view")
    @GetMapping()
    public String customerGroup()
    {
        return prefix + "/customerGroup";
    }

    /**
     * 查询行业客户配置列表
     */
    @RequiresPermissions("ecard:customerGroup:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardCustomerGroupInfo ecardCustomerGroupInfo)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardCustomerGroupInfo.setDictDeptId(sysDept.getDeptId());
        startPage();
        List<EcardCustomerGroupInfo> list = ecardCustomerGroupInfoService.selectEcardCustomerGroupInfoList(ecardCustomerGroupInfo);
        return getDataTable(list);
    }

    /**
     * 导出行业客户配置列表
     */
    @RequiresPermissions("ecard:customerGroup:export")
    @Log(title = "行业客户配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardCustomerGroupInfo ecardCustomerGroupInfo)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardCustomerGroupInfo.setDictDeptId(sysDept.getDeptId());
        List<EcardCustomerGroupInfo> list = ecardCustomerGroupInfoService.selectEcardCustomerGroupInfoList(ecardCustomerGroupInfo);
        ExcelUtil<EcardCustomerGroupInfo> util = new ExcelUtil<EcardCustomerGroupInfo>(EcardCustomerGroupInfo.class);
        return util.exportExcel(list, "行业客户配置数据");
    }

    /**
     * 新增行业客户配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存行业客户配置
     */
    @RequiresPermissions("ecard:customerGroup:add")
    @Log(title = "行业客户配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardCustomerGroupInfo ecardCustomerGroupInfo)
    {
        ecardCustomerGroupInfo.setCreatorId(ShiroUtils.getSysUser().getUserId());
        return toAjax(ecardCustomerGroupInfoService.insertEcardCustomerGroupInfo(ecardCustomerGroupInfo));
    }

    /**
     * 修改行业客户配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EcardCustomerGroupInfo ecardCustomerGroupInfo = ecardCustomerGroupInfoService.selectEcardCustomerGroupInfoById(id);
        mmap.put("ecardCustomerGroupInfo", ecardCustomerGroupInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存行业客户配置
     */
    @RequiresPermissions("ecard:customerGroup:edit")
    @Log(title = "行业客户配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardCustomerGroupInfo ecardCustomerGroupInfo)
    {
        return toAjax(ecardCustomerGroupInfoService.updateEcardCustomerGroupInfo(ecardCustomerGroupInfo));
    }

    /**
     * 删除行业客户配置
     */
    @RequiresPermissions("ecard:customerGroup:remove")
    @Log(title = "行业客户配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardCustomerGroupInfoService.deleteEcardCustomerGroupInfoByIds(ids));
    }

    /**
     * 行业客户配置状态修改
     * @param ecardCustomerGroupInfo
     * @return
     */
    @RequiresPermissions("ecard:customerGroup:edit")
    @Log(title = "行业客户配置状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(EcardCustomerGroupInfo ecardCustomerGroupInfo) {
        return toAjax(ecardCustomerGroupInfoService.updateEcardCustomerGroupInfo(ecardCustomerGroupInfo));
    }
}
